Crate fast_float

Source
Expand description

This crate provides a super-fast decimal number parser from strings into floats.

§Usage

There’s two top-level functions provided: parse and parse_partial, both taking either a string or a bytes slice and parsing the input into either f32 or f64:

  • parse treats the whole string as a decimal number and returns an error if there are invalid characters or if the string is empty.
  • parse_partial tries to find the longest substring at the beginning of the given input string that can be parsed as a decimal number and, in the case of success, returns the parsed value along the number of characters processed; an error is returned if the string doesn’t start with a decimal number or if it is empty. This function is most useful as a building block when constructing more complex parsers, or when parsing streams of data.

§Examples

// Parse the entire string as a decimal number.
let s = "1.23e-02";
let x: f32 = fast_float::parse(s).unwrap();
assert_eq!(x, 0.0123);

// Parse as many characters as possible as a decimal number.
let s = "1.23e-02foo";
let (x, n) = fast_float::parse_partial::<f32, _>(s).unwrap();
assert_eq!(x, 0.0123);
assert_eq!(n, 8);
assert_eq!(&s[n..], "foo");

Structs§

  • Opaque error type for fast-float parsing functions.

Traits§

  • Trait for numerical float types that can be parsed from string.

Functions§

  • Parse a decimal number from string into float (full).
  • Parse a decimal number from string into float (partial).

Type Aliases§

  • Result type alias for fast-float parsing functions.